package graphicView;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;

import model.*;
import java.util.Observable;

//ClassGraphicObject is built with ClassImpl object. Run ClassGraphicObject.drawClass(g) where g is the graphics object of the JPanel.
/**
 * Team EBE - Elite Bombard Elephants
 * 
 * ClassGraphicObject.java is the implementation of a graphical
 * object representing the class
 * 
 */
public class RelationshipGraphicObject extends Observable {
	protected RelationshipImpl relationshipObject;
	protected boolean active = false;
	private final int ENDPOINTSIZE = 20;
	private final int PADDING = ENDPOINTSIZE * 2;

	/**
	 * RelationshipGraphicObeject creates the relationship object
	 * 
	 * @param relationshipObjectIn
	 *            passes in the relationship object
	 */
	public RelationshipGraphicObject(RelationshipImpl relationshipObjectIn) {
		this.relationshipObject = relationshipObjectIn;
		relationshipObject.setType("agro");
	}

	/**
	 * getModel gets the model and returns the relationship object
	 * 
	 * @return relationshipObject returns the relationship object
	 */
	public RelationshipImpl getModel() {
		return relationshipObject;
	}

	/**
	 * activate simply activates the relationship object
	 */
	public void activate() {
		active = true;
	}

	/**
	 * deactivate simply deactivates the relationship object
	 */
	public void deactivate() {
		active = false;
	}

	/**
	 * isActive determines if a relationship is active
	 * 
	 * @return active returns a boolean value stating that a relationship is
	 *         active
	 */
	public boolean isActive() {
		return active;
	}

	/**
	 * sendToInspector sends the relationship object to the inspector and
	 * notifies the observers
	 */
	public void sendToInspector() {
		System.out.println("bleck");
		setChanged();
		notifyObservers(this.relationshipObject);
	}

	/**
	 * drawEndPoint draws the end-points of the relationships
	 * 
	 * @param g2d
	 *            passes in the 2d graphics
	 * @param xDest
	 *            passes in the desired x destination
	 * @param yDest
	 *            passes in the desired y destination
	 * @param type
	 *            passes in the string type
	 * @param anchorPointID
	 *            passes in an anchor point identification number
	 */
	private void drawEndPoint(Graphics2D g2d, int xDest, int yDest,
			String type, int anchorPointID) {
		Polygon p = new Polygon();
		if (type == "agro" || type == "comp") {

			if (anchorPointID == 1) {
				p.addPoint(xDest + ENDPOINTSIZE / 2, yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint((xDest - ENDPOINTSIZE / 2), yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest - (ENDPOINTSIZE * 2));
			} else if (anchorPointID >= 2 && anchorPointID <= 4) {
				p.addPoint(xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE / 2);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest + ENDPOINTSIZE, yDest + ENDPOINTSIZE / 2);
				p.addPoint(xDest + (ENDPOINTSIZE * 2), yDest);

			} else if (anchorPointID == 5) {
				p.addPoint(xDest + ENDPOINTSIZE / 2, yDest + ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest - ENDPOINTSIZE / 2, yDest + ENDPOINTSIZE);
				p.addPoint(xDest, yDest + (ENDPOINTSIZE * 2));
			} else if (anchorPointID == 0 || anchorPointID == 6
					|| anchorPointID == 7) {

				p.addPoint(xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE / 2);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest - ENDPOINTSIZE, yDest + ENDPOINTSIZE / 2);
				p.addPoint(xDest - (ENDPOINTSIZE * 2), yDest);

			}
			if (type == "agro") {

				g2d.setColor(Color.white);
				g2d.fillPolygon(p);
				g2d.setColor(Color.black);
				g2d.drawPolygon(p);
			} else {
				g2d.fillPolygon(p);
			}
		} else if (type == "general") {

			if (anchorPointID == 1) {
				p.addPoint(xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE);
			} else if (anchorPointID >= 2 && anchorPointID <= 4) {
				p.addPoint(xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest + ENDPOINTSIZE, yDest + ENDPOINTSIZE);

			} else if (anchorPointID == 5) {
				p.addPoint(xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE);

			} else if (anchorPointID == 0 || anchorPointID == 6
					|| anchorPointID == 7) {

				p.addPoint(xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				p.addPoint(xDest, yDest);
				p.addPoint(xDest - ENDPOINTSIZE, yDest + ENDPOINTSIZE);
			}
			g2d.drawPolygon(p);

		} else if (type == "depend") {
			if (anchorPointID == 1) {
				g2d.drawLine(xDest, yDest, xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				g2d.drawLine(xDest, yDest, xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE);
			} else if (anchorPointID >= 2 && anchorPointID <= 4) {
				g2d.drawLine(xDest, yDest, xDest + ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				g2d.drawLine(xDest, yDest, xDest + ENDPOINTSIZE, yDest + ENDPOINTSIZE);
			} else if (anchorPointID == 5) {
				g2d.drawLine(xDest, yDest, xDest - ENDPOINTSIZE, yDest + ENDPOINTSIZE);
				g2d.drawLine(xDest, yDest, xDest + ENDPOINTSIZE, yDest + ENDPOINTSIZE);
			} else if (anchorPointID == 0 || anchorPointID == 6
					|| anchorPointID == 7) {
				g2d.drawLine(xDest, yDest, xDest - ENDPOINTSIZE, yDest - ENDPOINTSIZE);
				g2d.drawLine(xDest, yDest, xDest - ENDPOINTSIZE, yDest + ENDPOINTSIZE);
			}

		} else {
			if (anchorPointID == 1) {

			} else if (anchorPointID >= 2 && anchorPointID <= 4) {

			} else if (anchorPointID == 5) {

			} else if (anchorPointID == 0 || anchorPointID == 6
					|| anchorPointID == 7) {

			}
		}
	}

	/**
	 * drawRelationship draws the relationship objects for the project
	 * 
	 * @param g passes in the graphics used to draw the relationships
	 */
	public void drawRelationship(Graphics g) {

		int xSrc = relationshipObject.getSrc().x;
		int xDest = relationshipObject.getDest().x;
		int ySrc = relationshipObject.getSrc().y;
		int yDest = relationshipObject.getDest().y;
		int xMid = (xSrc + xDest) / 2;
		Graphics2D g2d = (Graphics2D) g;
	
		if (relationshipObject.getSrc().getId() == 1) {
			g.drawLine(xSrc, ySrc, xSrc, ySrc -= PADDING);
		} else if (relationshipObject.getSrc().getId() == 5) {
			g.drawLine(xSrc, ySrc, xSrc, ySrc += PADDING);
		}

		g.drawLine(xSrc, ySrc, xMid, ySrc);
		if (relationshipObject.getDest().getId() == 1) {
			g.drawLine(xMid, ySrc, xMid, yDest - PADDING);
			g.drawLine(xMid, yDest - PADDING, xDest, yDest - PADDING);
			g.drawLine(xDest, yDest - PADDING, xDest, yDest);
		} else if (relationshipObject.getDest().getId() == 5) {
			g.drawLine(xMid, ySrc, xMid, yDest + PADDING);
			g.drawLine(xMid, yDest + PADDING, xDest, yDest + PADDING);
			g.drawLine(xDest, yDest + PADDING, xDest, yDest);
		} else {
			g.drawLine(xMid, ySrc, xMid, yDest);
			g.drawLine(xMid, yDest, xDest, yDest);
		}

		drawEndPoint(g2d, xDest, yDest, relationshipObject.getType(),
				relationshipObject.getDest().getId());
	}

}
